/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package administrador;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.IOException;

/** * * @author wakam.cha.hanba@gmail.com */

public class Backup {

public static boolean backup(String mysqldumpPath, String backupPath) {
    boolean status = false;
    String username = "root";
    String password = "yxes1984";
    String database = "bdmultiadmin";


    String command = mysqldumpPath + "\\mysqldump -u " + username + " -p " + password + " " + database + " -r " + backupPath;
    try {
        Process runtimeProcess = Runtime.getRuntime().exec(command);
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
           //System.out.println("DatabaseManager.backup: Backup Successfull");
            status = true;
        } else {
           //System.out.println("DatabaseManager.backup: Backup Failure!");
        }
    } catch (IOException ioe) {
       //System.out.println("Exception IO");
        ioe.printStackTrace();
    } catch (Exception e) {
       //System.out.println("Exception");
        e.printStackTrace();
    }
    return status;
}
public static boolean restore(String filePath){
    boolean status = false;
    String username = "daniel1";
    String password = "yxes1984";
    String[] command = new String[]{"mysql", "database_name", "-u" + username, "-p" + password, "-e", " source "+filePath };

    try {
        Process runtimeProcess = Runtime.getRuntime().exec(command);
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
           //System.out.println("DatabaseManager.restore: Restore Successfull");
            status = true;
        } else {
           //System.out.println("DatabaseManager.restore: Restore Failure!");
        }
    } catch (IOException ioe) {
       //System.out.println("Exception IO");
        ioe.printStackTrace();
    } catch (Exception e) {
       //System.out.println("Exception");
        e.printStackTrace();
    }
    return status;
}

//for testing
public static void main(String args[]){
    String backupName = "C:\\backupHvs.sql";
    Backup b= new Backup();//C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql
    b.backup("C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin", backupName);
//    this.restore(backupName);
}
}